package com.duoketech.service.weixin;

import com.duoketech.exception.ValidateException;
import com.duoketech.model.receiver.Receiver;
import com.duoketech.utils.validator.Validator;
import com.duoketech.utils.validator.rules.string.StringMustBeRequired;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;

public class ReceiverService {

    public static ReceiverService ME = new ReceiverService();


    public Page<Receiver> queryReceiverList(int pageNum,int pageSize,int memberId){
        String sql="from "+Receiver.dao.tableName()+" where member_id = ? and status=? order by id desc";
        Page<Receiver> receiverPage = Receiver.dao.paginate(pageNum,pageSize,"select * ",sql,memberId,Receiver.STATUS_NORMAL);
        return receiverPage;
    }

    public void markerReceiverAsDefault(int id ,int memberId) throws ValidateException{
        Receiver receiver = queryReceiverById(id);
        if(receiver==null){
            throw new ValidateException("当前记录不存在");
        }

        if(receiver.getInt(Receiver.MEMBER_ID)!=memberId){
            throw new ValidateException("权限不够，无法进行当前操作");
        }
        String sql="update "+Receiver.dao.tableName()+" set is_default=?  where member_id=?";
        Db.update(sql,Receiver.DEFAULT_NO,memberId);
        receiver.set(Receiver.IS_DEFAULT,Receiver.DEFAULT_YES);
        receiver.update();
    }

    public void delReciver(int id ,int memberId) throws ValidateException {
        Receiver receiver = queryReceiverById(id);
        if(receiver==null){
            throw new ValidateException("当前记录不存在");
        }

        if(receiver.getInt(Receiver.MEMBER_ID)!=memberId){
            throw new ValidateException("权限不够，无法进行当前操作");
        }

        receiver.set(Receiver.STATUS,Receiver.STATUS_DEL);
        receiver.update();
    }

    public Receiver queryReceiverById(int id ){
        Receiver receiver = Receiver.dao.findById(id);
        return receiver;
    }


    public void doUpdateReceiver(Receiver receiver,int memberId) throws ValidateException {

        Validator validator = new Validator();


        String name = receiver.getStr(Receiver.NAME);
        String mobile = receiver.getStr(Receiver.MOBILE);
        String address = receiver.getStr(Receiver.ADDRESS);
        String province = receiver.getStr(Receiver.PROVINCE);
        String city = receiver.getStr(Receiver.CITY);
        String area = receiver.getStr(Receiver.AREA);

        validator.addRule(new StringMustBeRequired(name,"请输入收货人姓名"));
        validator.addRule(new StringMustBeRequired(mobile,"请输入收货人手机号码"));
        validator.addRule(new StringMustBeRequired(address,"请输入收货人详细地址"));
        validator.addRule(new StringMustBeRequired(province,"请选择省份"));
        validator.addRule(new StringMustBeRequired(city,"请选择市区"));
        validator.addRule(new StringMustBeRequired(area,"请选择区域"));

        validator.excute();
        receiver.set(Receiver.MEMBER_ID,memberId);
        String areaDetail = receiver.getStr(Receiver.PROVINCE)
                +receiver.getStr(Receiver.CITY)
                +receiver.getStr(Receiver.AREA) +receiver.getStr(Receiver.ADDRESS);
        receiver.set(Receiver.AREA_DETAIL,areaDetail);
        receiver.set(Receiver.MODIFY_TIME,new Date());
        receiver.update();
    }


    public void doAddReceiver(Receiver receiver,int memberId) throws ValidateException {

        Validator validator = new Validator();
        String name = receiver.getStr(Receiver.NAME);
        String mobile = receiver.getStr(Receiver.MOBILE);
        String address = receiver.getStr(Receiver.ADDRESS);
        String province = receiver.getStr(Receiver.PROVINCE);
        String city = receiver.getStr(Receiver.CITY);
        String area = receiver.getStr(Receiver.AREA);

        validator.addRule(new StringMustBeRequired(name,"请输入收货人姓名"));
        validator.addRule(new StringMustBeRequired(mobile,"请输入收货人手机号码"));
        validator.addRule(new StringMustBeRequired(address,"请输入收货人详细地址"));
        validator.addRule(new StringMustBeRequired(province,"请选择省份"));
        validator.addRule(new StringMustBeRequired(city,"请选择市区"));
        validator.addRule(new StringMustBeRequired(area,"请选择区域"));
        validator.excute();

        Receiver temp=queryDefaultReceiverBy(memberId);

        if(temp==null){
            receiver.set(Receiver.IS_DEFAULT,Receiver.DEFAULT_YES);
        }else{
            receiver.set(Receiver.IS_DEFAULT,Receiver.DEFAULT_NO);
        }

        String areaDetail = receiver.getStr(Receiver.PROVINCE)
                +receiver.getStr(Receiver.CITY)
                +receiver.getStr(Receiver.AREA) +receiver.getStr(Receiver.ADDRESS);

        receiver.set(Receiver.MEMBER_ID,memberId);
        receiver.set(Receiver.STATUS,Receiver.STATUS_NORMAL);
        receiver.set(Receiver.MEMBER_ID,memberId);
        receiver.set(Receiver.CREATE_TIME,new Date());
        receiver.set(Receiver.AREA_DETAIL,areaDetail);
        receiver.set(Receiver.MODIFY_TIME,new Date());
        receiver.save();
    }


    public Receiver queryDefaultReceiverBy(int memberId){
        String sql="select * from "+Receiver.dao.tableName()+" where is_default=? and status=? and member_id=? limit 1";
        return Receiver.dao.findFirst(sql,Receiver.DEFAULT_YES,Receiver.STATUS_NORMAL,memberId);
    }
}
